package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer saveComment(Comment comment) {  // 1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞

        ObjectId publishId = comment.getPublishId(); // publishId有可能是圈子id  小视频id 评论id   comment.getCommentType()123  45  6

        Integer commentType = comment.getCommentType();

        if(commentType==1||commentType==2||commentType==3){  //操作圈子
//        1、修改movement_detail表中的点赞数据 +1     sql: update movement_detail set likeCount=likeCount+1 where id=???

            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1);
            mongoTemplate.updateFirst(query,update,Movement.class);
//            Movement movement1 = mongoTemplate.findById(publishId, Movement.class);    //查询圈子



//        2、给comment的三个属性上赋值
            comment.setId(new ObjectId());
            comment.setCreated(new Date().getTime());
            Movement movement = mongoTemplate.findById(publishId, Movement.class);    //查询圈子
            comment.setPublishUserId(movement.getUserId());//圈子的发布人

//       3、保存 comment
            mongoTemplate.save(comment);
//            返回点赞、评论、喜欢数据
            return  comment.getCount(movement);

        }else if(commentType==4||commentType==5){
//            操作小视频  TODO

            return 0;
        }else{
//            操作评论  TODO
            return 0;
        }
    }

    @Override
    public Integer deleteComment(Comment comment) {  // 1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        ObjectId publishId = comment.getPublishId(); // publishId有可能是圈子id  小视频id 评论id   comment.getCommentType()123  45  6

        Integer commentType = comment.getCommentType();

        if(commentType==1||commentType==2||commentType==3){  //操作圈子
//        1、修改movement_detail表中的点赞数据 -1     sql: update movement_detail set likeCount=likeCount-1 where id=???

            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),-1);
            mongoTemplate.updateFirst(query,update,Movement.class);
            Movement movement = mongoTemplate.findById(publishId, Movement.class);    //查询圈子



//       2、删除 comment  delete from comment where userId=?? and publishId=?? and commentType=??
            Query query1 = new Query(Criteria.where("userId").is(comment.getUserId()).and("publishId").is(publishId).and("commentType").is(comment.getCommentType()));
            mongoTemplate.remove(query1,Comment.class);
//            返回点赞、评论、喜欢数据
            return  comment.getCount(movement);

        }else if(commentType==4||commentType==5){
//            操作小视频  TODO

            return 0;
        }else{
//            操作评论  TODO
            return 0;
        }
    }

    @Override
    public PageResult queryComments(String movementId, Integer page, Integer pagesize) {
//        查询指定圈子的评论 SQL：select * from comment where publishId=#{movementId} and commentType=2  limit (page-1)*pagesize,pagesize order by created desc

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

    @Override
    public PageResult queryLikesOrLovesOrComments(Long userId, Integer page, Integer pagesize, Integer commentType) {
//        select * from  comment where  publishUserId=1  and commentType=1
        Query query = new Query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }
}
